package cn.wolfcode.luowowo.controller;

import cn.wolfcode.luowowo.annotation.RequireLogin;
import cn.wolfcode.luowowo.annotation.UserParam;
import cn.wolfcode.luowowo.domain.*;
import cn.wolfcode.luowowo.query.StrategyCommentQuery;
import cn.wolfcode.luowowo.query.StrategyQuery;
import cn.wolfcode.luowowo.query.TravelCommentQuery;
import cn.wolfcode.luowowo.redis.IStrategyRedisVO;
import cn.wolfcode.luowowo.redis.impl.StrategyRedisVOImpl;
import cn.wolfcode.luowowo.redis.util.RedisKeys;
import cn.wolfcode.luowowo.redis.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.service.*;
import cn.wolfcode.luowowo.util.JsonResult;
import cn.wolfcode.luowowo.util.ParamMap;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/strategy")
public class StrategyController {
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IStrategyCommentService strategyCommentService;
    @Autowired
    private IStrategyCatalogService strategyCatalogService;
    @Autowired
    private IDestinationService destinationService;
    @GetMapping("/queryCatalogsByDestId")
    public Object queryCatalogsByDestId(String destId){
        List<StrategyCatalog> byDestId = strategyCatalogService.findByDestId(destId);
        return JsonResult.success(byDestId);
    }

    @Autowired
    private IStrategyRedisVO strategyRedisVO;
    /**
     *
     * @param id  攻略的id
     * @return
     */
    @GetMapping("/detail")
    public Object detail(String id,@UserParam UserInfo userInfo){
        Strategy strategy = strategyService.get(id);
        StrategyCommentQuery qo = new StrategyCommentQuery();
        //设置查询条件是哪个攻略的评论
        qo.setStrategyId(id);
        Page page = strategyCommentService.list(qo);
        StrategyStatisVO vo = strategyRedisVO.addViewnum(id);

        //sids存放该登陆者的所有收藏的攻略id
        List<String> sids = new ArrayList<>();
        if (userInfo != null){
            String key = RedisKeys.USER_STRATEGY_FAVOR.join(userInfo.getId());
            String value = redisTemplate.opsForValue().get(key);
            sids = JSON.parseArray(value, String.class);
        }
        return JsonResult.success(
                new ParamMap()
                        .put("strategy",strategy)
                        .put("page",page)
                        .put("vo",vo)
                        .put("sids",sids)
        );
    }
    @Autowired
    private IStrategyThemeService strategyThemeService;
    @GetMapping("/list")
    public Object list(StrategyQuery qo){
        //page
        Page page = strategyService.list(qo);
        //themes
        List<StrategyTheme> themes = strategyThemeService.list();
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        return JsonResult.success(
                new ParamMap()
                        .put("page",page)
                        .put("themes",themes)
                        .put("toasts",toasts)
        );
    }
    @GetMapping("/query")
    public Object query(StrategyQuery qo){
        //page
        Page page = strategyService.list(qo);
        return JsonResult.success(page);
    }

    /**
     *  攻略评论
     * @param strategyComment
     * @param userInfo
     * @return
     */
    @PostMapping("/addComment")
    @RequireLogin
    public Object addComment(StrategyComment strategyComment, @UserParam UserInfo userInfo){
        //相同属性名的赋值  id也会赋值  userId赋不上值
        BeanUtils.copyProperties(userInfo,strategyComment);
        strategyComment.setUserId(userInfo.getId());
        strategyComment.setId(null);
        strategyCommentService.save(strategyComment);

        StrategyCommentQuery qo = new StrategyCommentQuery();
        //设置查询条件是哪个攻略的评论
        qo.setStrategyId(strategyComment.getStrategyId());
        Page page = strategyCommentService.list(qo);
        StrategyStatisVO vo = strategyRedisVO.addReplynum(strategyComment.getStrategyId());
        return JsonResult.success(new ParamMap().put("page",page).put("vo",vo));
    }

    /**
     *  攻略评论点赞
     * @param cid 该条评论的id
     * @param sid 评论的攻略id
     * @return
     */
    @PostMapping("/commentThumb")
    @RequireLogin
    public Object commentThumb(String cid,String sid,@UserParam UserInfo userInfo){
        strategyCommentService.commentThumb(cid,userInfo);

        StrategyCommentQuery qo = new StrategyCommentQuery();
        //设置查询条件是哪个攻略的评论
        qo.setStrategyId(sid);
        Page page = strategyCommentService.list(qo);
        return JsonResult.success(page);
    }
    @Autowired
    private StringRedisTemplate redisTemplate;
    @PostMapping("/favor")
    @RequireLogin
    public Object favor(String sid,@UserParam UserInfo userInfo){
        boolean ret = strategyRedisVO.addFavornum(sid,userInfo.getId());
        StrategyStatisVO vo = strategyRedisVO.getVO(sid);

        //获取的登陆者收藏的sid
        String key = RedisKeys.USER_STRATEGY_FAVOR.join(userInfo.getId());
        String value = redisTemplate.opsForValue().get(key);
        List<String> sids = JSON.parseArray(value, String.class);
        return JsonResult.success(new ParamMap()
                .put("vo",vo)
                .put("ret",ret)
                .put("sids",sids)
        );
    }

    @GetMapping("/commentList")
    public Object commentList(StrategyCommentQuery qo){
        //设置查询条件是哪个游记的评论
        Page page = strategyCommentService.list(qo);
        return JsonResult.success(page);
    }

    @PostMapping("/strategyThumbup")
    @RequireLogin
    public Object strategyThumbup(String sid,@UserParam UserInfo userInfo){
        boolean ret = strategyRedisVO.addThumbup(sid,userInfo.getId());
        return JsonResult.success(new ParamMap()
                .put("ret",ret)
                .put("vo",strategyRedisVO.getVO(sid))
        );
    }

}
